#!/sbin/openrc-run
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
#
# This file is tasked with testing for the existence of the ibmvscsis driver
# and configuring the ibmvscsi server properly as indicated by the config file
# located at /etc/ibmvscsis.conf
#

DRIVER=ibmvscsis
SYSFS=/sys/bus/vio/drivers/ibmvscsis
CONFIG=/etc/ibmvscsis.conf

depend() {
	need logger
	provide ibmvscsis
}

checkconfig() {
    if [ ! -e ${CONFIG} ] ; then
        eerror " ${CONFIG} does not exist."
        return 1
    fi
}

checkmodule_load() {
    # The existence of $SYSFS indicates that the module has been loaded or that
    # the driver is at least built into the kernel.
    if [ ! -e ${SYSFS} ] ; then
        ewarn " Module ${DRIVER} is not loaded, attempting to load it"
        /sbin/modprobe ${DRIVER} &> /dev/null && return 0
        eerror "  Failed to load module ${DRIVER}"
        return 1
    fi
}

checkmodule() {
    # The existence of $SYSFS indicates that the module has been loaded or that
    # the driver is at least built into the kernel.
    if [ ! -e ${SYSFS} ] ; then
        eerror " Module ${DRIVER} is not loaded"
        return 1
    fi
}

start() {
    ebegin "Starting vscsiadmin"
    checkconfig || return 1
    checkmodule_load || return 1
    /usr/sbin/vscsiadmin -start &> /dev/null
    eend $? "Failed to start vscsiadmin"
}

stop() {
    ebegin "Stopping vscsiadmin"
    checkmodule || return 1
    /usr/sbin/vscsiadmin -stop &> /dev/null
    eend $? "Failed to stop vscsiadmin"
}

status() {
    checkmodule || return 1
    /usr/sbin/vscsiadmin -status
}

restart() {
    stop
    start
}
